import csv, os, math, statistics



def main():

    global total_trans_list
    global total_rate_list
    
    global early_trans_list
    global early_rate_list
    
    global later_trans_list
    global later_rate_list
    global path
    total_trans_list=[]
    total_rate_list=[]
    
    early_trans_list=[]
    early_rate_list=[]
    
    later_trans_list=[]
    later_rate_list=[]
    path = "E:\\cp\\csv\\"
    print""
    print"Okay"
    print""
    
    writer= open("E:\\cp\\csv\\testStd.csv", 'w')
    for i in range(1,13):
        writer.write("ISLAND "+str(i)+"\n\n"+"Transect,1850+,1925+,1970+\n")
        total(i)
        early(i)
        later(i)
        totalc=0
        for z in total_trans_list:
            writer.write(str(z)+","+str(total_rate_list[totalc]+","))

            earlyc=0
            switch=0
            for y in early_trans_list:
                if str(y)==str(z):
                    writer.write(early_rate_list[earlyc]+",")
                    switch=1
                earlyc=earlyc+1
            if switch==0:
                writer.write("NULL,")
                
            laterc=0
            switch=0
            for q in later_trans_list:
                if str(q)==str(z):
                    writer.write(later_rate_list[laterc]+"\n")
                    switch=1
                laterc=laterc+1
            if switch==0:
                writer.write("NULL\n")
            
            
            totalc=totalc+1
        writer.write("\n\n\n")
            
        
        del total_trans_list[:]
        del total_rate_list[:]
    
        del early_trans_list[:]
        del early_rate_list[:]
    
        del later_trans_list[:]
        del later_rate_list[:]
    writer.close()
   
    openIt()
   

    

    
  
def total(num):
    
    islandTotal='r'+str(num)+".csv"
    with open(path+islandTotal,'rb') as csvTotal:
        total = csv.reader(csvTotal)
        for x in total:
                        
                
            if (str(x[8]) != "-1.#IND") & (str(x[8]) != "LRR"):
                total_trans_list.append(x[1])
                total_rate_list.append(x[8])
            print num, statistics.stdev(total_rate_list)
 
def early(num):

    island='Rates'+str(num)+".csv"
    with open(path+island,'rb') as csvTotal:
        total = csv.reader(csvTotal)
        for x in total:
                        
                
            if (str(x[8]) != "-1.#IND") & (str(x[8]) != "LRR"):
                early_trans_list.append(x[1])
                early_rate_list.append(x[8])
  

def later(num):

    island='later'+str(num)+".csv"
    with open(path+island,'rb') as csvTotal:
        total = csv.reader(csvTotal)
        for x in total:
                   
                
            if (str(x[8]) != "-1.#IND") & (str(x[8]) != "LRR"):
                later_trans_list.append(x[1])
                later_rate_list.append(x[8])
    
   
        
            
def openIt():
    os.startfile("E:\\cp\\csv\\compare.csv")


          
main()
